<?php
namespace Common\Model;
use Common\Model\VRNewsModel;
class NewsColumnModel extends VRNewsModel
{
	
	protected $_validate = array(
		//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
	);

	//获取栏目列表
	public function Getall($pageStart,$pageSize){
		$column = new NewsColumnModel();
		$list_con = $column
			->join('vrnews1.news_column_admin on vrnews1.news_column.column_id = vrnews1.news_column_admin.column_id','left')
			->join('vradmin1.admin_user on vrnews1.news_column_admin.admin_id = vradmin1.admin_user.admin_id','left')
			->where("news_column.status = 1")
			->Group("news_column.column_id")
			->select();

		$list = $column
			->join('vrnews1.news_column_admin on vrnews1.news_column.column_id = vrnews1.news_column_admin.column_id','left')
			->join('vradmin1.admin_user on vrnews1.news_column_admin.admin_id = vradmin1.admin_user.admin_id','left')
			->where("news_column.status = 1")
			->field("*,news_column.column_id,group_concat( vradmin1.admin_user.admin_id ) admin_ids,group_concat(vradmin1.admin_user.username ) usernames,news_column.type  ")
			->Group("news_column.column_id")
			->Order("weight desc,news_column.create_time desc")
			->limit($pageStart, $pageSize)
			->select();
		$res['count'] = count($list_con);
		$res['list']  = $list;
		return $res;
	}

/*
 * SELECT *,news_column.column_id,
group_concat( vradmin1.admin_user.admin_id ) admin_ids,
group_concat(vradmin1.admin_user.username )  usernames
FROM vrnews1.news_column
left join vrnews1.news_column_admin
on vrnews1.news_column.column_id = vrnews1.news_column_admin.column_id
left join vradmin1.admin_user on news_column_admin.admin_id = vradmin1.admin_user.admin_id
group by news_column.column_id
 * */

	//普通管理员 查看自己栏目
	public function GetColumns($admin_id){
		$column = new NewsColumnAdminModel();

		$columns = $column
			->join("vrnews1.news_column on news_column_admin.column_id = vrnews1.news_column.column_id")
			->where("news_column_admin.admin_id = $admin_id")
			->field("news_column_admin.column_id,name")
			->select();
		return $columns;
	}

	//编辑 栏目内容
	public function Edit($type,$id,$value){
		$column = new NewsColumnModel();
		$data = array();
		if($type == "column_id"){
			//栏目名更改
			$data['name'] = $value;
			$res = $column
				->where("column_id = $id")
				->save($data);
		}else if($type == "weight"){
			//权重更改
			$data['weight'] = $value;
			$res = $column
				->where("column_id = $id")
				->save($data);
		}else if($type == "username"){
//			$data['']
		}
		return $res;
	}

	//删除 栏目
	public function colDel($column_id){
		if($column_id){
			$column = new NewsColumnModel();
			$data['status'] = 0;
			$delid = $column->where('column_id = '.$column_id)->data($data)->save();

			if($delid){
				//删除栏目所属的管理员
				$column_admin_model = new NewsColumnAdminModel();
				$have_admin = $column_admin_model->where('column_id = '.$column_id)->select();
				if($have_admin) {
					$rid = $column_admin_model->where('column_id = ' . $column_id)->delete();

				}
				//同时 删除栏目对应的直播管理员
				$column_live = new ColumnLiveAdminModel();
				$have_live = $column_live->where('column_id = '.$column_id .' and type = 0')->select();
				if($have_live){
					$dellive = $column_live->where('column_id = ' . $column_id.' and type = 0')->delete();
				}
				if(!$rid || !$dellive){
					return false;
				}else{
					return true;
				}
			}

		}else {
			return false;
		}
	}
}

